import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import {
    getCookie,
    setCookie
} from '@/common/js/cookie';
import {
    Message,
    MessageBox
} from 'element-ui';
import {
    _getUrlKey
} from "@/utils/common.js"
import {
    setSessionStorage,
    getSessionStorage,
    removeSessionStorage
} from "@/common/js/storage";
import Watermark from '@/common/js/watermark'; //加水印
import { getRequestParams } from "@/common/js/fastInquiryFn2";

import { $budgetAPI } from '@/api';



const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
    return originalPush.call(this, location).catch(err => err)
}

// 客户登录
const component = {
    login: r => require.ensure([], () => r(require('@/views/login')), 'login'),
    loginForzjhc: r => require.ensure([], () => r(require('@/views/loginForzjhc')), 'loginForzjhc'),
    layout: r => require.ensure([], () => r(require('@/components/layout')), 'layout'),
    virtualLogin: r => require.ensure([], () => r(require('@/views/budget/virtualLogin')), 'virtualLogin') // 虚拟登录
};


Vue.use(Router);
const routers = [{
    path: '',
    redirect: '/login'
},
{
    path: '/',
    redirect: '/login'
},
{
    path: '/budget',
    name: 'budget',
    component: component.layout,
    children: []
},
{
    path: '/login',
    name: 'login',
    component: component.login,
    meta: {
        title: '登录',
        requireLogin: false
    }
},
{
    path: '/loginForzjhc',
    name: 'loginForzjhc',
    component: component.loginForzjhc,
    meta: {
        title: '登录',
        requireLogin: false
    }
},
{
    path: '/virtualLogin',
    name: 'virtualLogin',
    component: component.virtualLogin,
    meta: {
        title: '登录',
        requireLogin: false
    }
},
    // { path: '*', redirect: '/budget/home'} // 404 统一跳首页
];

function pushRouter (name, thisArray) {
    for (var i in routers) {
        if (routers[i].name == name) {
            for (var j in thisArray) {
                routers[i].children.push(thisArray[j]);
            }
        }
    }
}

// 客户登录 -- 引入模块路由添加进路由
import budget from './budget'
pushRouter('budget', budget);
const router = new Router({
    // mode: 'history',
    linkActiveClass: 'router-active',
    scrollBehavior: () => ({
        y: 0
    }),
    routes: routers
});



// const accountInfo = { "areaId": "5", "areaLvl": 3, "areaName": "嘉兴", "roleId": 1, "waterMark": "王树志:13306814207" }
// setCookie('token', 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwidXNlck1vYmlsZSI6IjE1Mzk3MTM0NjU4Iiwic3RhZmZJZCI6NzU4MCwianRpIjoiNDA4YWU5NDUtMTdlMS00OTE4LWI3NDUtMmM1YzFiZTZjOTFiIiwidXNlcm5hbWUiOiLkuo7lrablpY4iLCJpYXQiOjE2MDM2MTUzNTIsImV4cCI6MTYwMzcwMTc1Mn0.gWhm20EcDJRkfkQCtjGyichq4iXZJHK6Ly9PFUp8500')
// setCookie('token', 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwidXNlck1vYmlsZSI6IjE1Mzk3MTM0NjU4Iiwic3RhZmZJZCI6NzU4MCwianRpIjoiNDA4YWU5NDUtMTdlMS00OTE4LWI3NDUtMmM1YzFiZTZjOTFiIiwidXNlcm5hbWUiOiLkuo7lrablpY4iLCJpYXQiOjE2MDMxMjMzMjgsImV4cCI6MTYwMzIwOTcyOH0.pe8nCsqFQVBgZ8CYaKtPVBjULSO8uxWzZt8FkywtALc')
// window.sessionStorage.setItem('SysUserCode', 13306814207)
// window.sessionStorage.setItem('accountInfo', JSON.stringify(accountInfo))
// window.sessionStorage.setItem('userName', '王树志')
// 路由跳转之前
router.beforeEach((to, from, next) => {
    // console.log(to.meta.requireLogin)
    // 显示loading过渡效果
    // store.dispatch("ChangeLoading", true);
    // if (to.meta.requireLogin) {
    //     if (getCookie('token')) {
    //         next();
    //     } else {
    //         next('/login');
    //     }
    // } else {
    //     next();
    // }

    let token = getCookie('token');
    // if (!token) {
    //   debugger
    //   MessageBox({
    //     type: "error",
    //     title: "提示",
    //     message: "登录已超时，请重新登录"
    //   }).then(() => {
    //     router.push({
    //       path: '/login'
    //     })
    //   });
    // }
    // else
    if (!token && to.path == '/budget/virtualLogin') {
        window.JXtoken = window.location.href;
        window.JXtoken = getRequestParams();
        router.push({
            path: '/virtualLogin'
        });
    } else if (to.meta.requireLogin) {
        if (getCookie('token')) {
            next();
        } else if (!token && to.path == '/virtualLogin') {
            router.push({
                path: '/virtualLogin'
            });
        } else if (!token && to.path != '/login') {
            Message({
                type: 'error',
                message: '登录失效，请重新登录',
            })
            router.push({
                path: '/login'
            });
        }
    } else {
        next();
    }

    // if (!token && to.path == '/virtualLogin') {
    //   let phone = _getUrlKey('phone', window.location.href);
    //   let uuid = _getUrlKey('uuid', window.location.href);
    //   window.zjhc = {
    //     phone: phone,
    //     uuid: uuid
    //   }
    //   router.push({
    //     path: '/virtualLogin'
    //   });
    // } else if (!token && to.path != '/login') {
    //   Message({
    //     type: 'error',
    //     message: '登录失效，请重新登录',
    //   })
    //   router.push({
    //     path: '/login'
    //   });
    // }
    // next();

    // 更新页面title
    window.document.title = to.meta.title ? `${to.meta.title} - 收入分析系统` : '收入分析系统'
});


// 路由跳转之后
router.afterEach(function (to) {
    // 关闭loading过渡效果
    // Watermark.set(JSON.parse(getSessionStorage('accountInfo')).waterMark);
    // store.dispatch("ChangeLoading", false);
});
export default router;